//
//  WHTFirstPageControllerVC.m
//  WZLProduct
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "WHTFirstPageControllerVC.h"
#import "FistPageCollectionHeaderView_Ht.h"
#import "HTLBView.h"
#import "firstPageFirstSecctionCell_Ht.h"
#import "firstPageSecondSecctionCell_Ht.h"
#import "FirstPageFirstModel_Ht.h"

@interface WHTFirstPageControllerVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,HTLBDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, retain) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) FirstPageFirstModel_Ht *model;
@end

@implementation WHTFirstPageControllerVC

static NSString *firstPageFirstSecctionCell_id_Ht = @"firstPageFirstSecctionCell_id_Ht";
static NSString *firstPageSecondSecctionCell_id_Ht = @"firstPageSecondSecctionCell_id_Ht";
static NSString *fistPageCollectionHeaderIdentifier = @"fistPageCollectionHeaderIdentifier";


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.91 green:0.87 blue:0.78 alpha:1];
    [self requestDataByURL:KUrl_FistPage_WHT];
   
    [self setNavigationBar];
    [self creatCollectionView];


}

#pragma mark 请求数据
- (void)requestDataByURL:(NSString *)url{
    
    [RequestData GETRequestWithURLString:url completionWithSuccess:^(id result) {
        
        self.model = [[FirstPageFirstModel_Ht alloc] init];
        [self.model setValuesForKeysWithDictionary:result[@"data"]];
//        //刷新UI
//        [self.collectionView reloadData];
        
    } WithFailure:^(id error) {
        self.model = [[FirstPageFirstModel_Ht alloc] init];
        [self.model setValuesForKeysWithDictionary:error[@"data"]];
//        //刷新UI
//        [self.collectionView reloadData];
    }];

}

#pragma mark- 设置导航栏
- (void)setNavigationBar{
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HT_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)]];

    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 20, kScreenWidth - 100, 30)];
    self.navigationItem.titleView = textField;
    textField.placeholder = @"🔍搜索目的地.游记....";
    textField.textAlignment = NSTextAlignmentCenter;
    textField.textColor = [UIColor orangeColor];

    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"附近" style:UIBarButtonItemStylePlain target:self action:@selector(RightBarButtonAction:)]];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor orangeColor];

    
}
#pragma mark 返回
- (void)back:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 附近
- (void)RightBarButtonAction:(UIBarButtonItem *)sender{
    
}


#pragma mark- CollectionView
- (void)creatCollectionView{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.91 green:0.87 blue:0.78 alpha:1];
    self.collectionView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    //注册
    [self.collectionView registerClass:[firstPageFirstSecctionCell_Ht class] forCellWithReuseIdentifier:firstPageFirstSecctionCell_id_Ht];
    [self.collectionView registerClass:[firstPageSecondSecctionCell_Ht class] forCellWithReuseIdentifier:firstPageSecondSecctionCell_id_Ht];
    [self.collectionView registerClass:[FistPageCollectionHeaderView_Ht class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:fistPageCollectionHeaderIdentifier];
    //轮播
    [self setlbView];
}

/** 轮播图 */
- (void)setlbView{
    //轮播图
    HTLBView *lbView = [[HTLBView alloc] initWithFrame:CGRectMake(0, - 200, kScreenWidth, 200)];
    [self.collectionView addSubview:lbView];
    lbView.scrollView.backgroundColor = [UIColor redColor];
    lbView.htLBdelegate = self;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        firstPageFirstSecctionCell_Ht *cell = [collectionView dequeueReusableCellWithReuseIdentifier:firstPageFirstSecctionCell_id_Ht forIndexPath:indexPath];
        return cell;
    }
    firstPageSecondSecctionCell_Ht *cell = [collectionView dequeueReusableCellWithReuseIdentifier:firstPageSecondSecctionCell_id_Ht forIndexPath:indexPath];
    return cell;
    

}


/** 头视图 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        FistPageCollectionHeaderView_Ht *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:fistPageCollectionHeaderIdentifier forIndexPath:indexPath];
        [header.rightButton addTarget:self action:@selector(puchNextPage:) forControlEvents:UIControlEventTouchUpInside];
        if (indexPath.section == 0) {
            header.sectionTitle.text = @"每日精选故事";
            header.rightButton.hidden = NO;
            
        }else{
            header.sectionTitle.text = @"精彩原创和标题";
             header.rightButton.hidden = YES;
        }
        
         return header;
    }
    return nil;
}
/** 第一个分区头视图,右按钮,跳转下一页 */
- (void)puchNextPage:(UIButton *)sender{
    NSLog(@"跳啊");
}


/** Header大小 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kScreenWidth, 50);
}

/** 点击方法 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
}




#pragma mark FlowLayoutDelegate

/** 每个分区item的大小 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return  CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30) / 2, ([UIScreen mainScreen].bounds.size.width - 30) / 2 + 30);
        
    }else{
        return CGSizeMake([UIScreen mainScreen].bounds.size.width- 20, 150);
    }
}



@end
